package com.zhiwang.planet.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.View.MeasureSpec
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhiwang.planet.R
import com.zhiwang.planet.util.dp

class EqualLinearLayoutManager : LinearLayoutManager {

    private var screenWidth = 0

    private var equalCount = 3

    constructor(context: Context?) : this(context, LinearLayout.HORIZONTAL, false)
    constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {


        val ta = context!!.obtainStyledAttributes(attrs, R.styleable.EqualLinearLayoutManager)
        ta.getInteger(R.styleable.EqualLinearLayoutManager_equal_count, 3)
        ta.recycle()
    }


    override fun measureChildWithMargins(child: View, widthUsed: Int, heightUsed: Int) {

        val layoutParams = child.layoutParams
        layoutParams.width = (getScreenWidth(child.context) - (equalCount + 1) * 10.dp) / 3
        super.measureChild(child, widthUsed, heightUsed)
        super.measureChildWithMargins(child, widthUsed, heightUsed)
    }


    fun getScreenWidth(context: Context): Int {

        if (screenWidth == 0) {
            screenWidth = context.resources.displayMetrics.widthPixels
        }

        return screenWidth
    }

}

class BottomAlignmentLinearLayoutManager : LinearLayoutManager {

    constructor(context: Context?) : this(context, LinearLayout.HORIZONTAL, false)
    constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun layoutDecoratedWithMargins(child: View, left: Int, top: Int, right: Int, bottom: Int) {

        //底部对齐

        val parentHeight = height
        val childHeight = child.measuredHeight

        val finaltop = parentHeight - childHeight

        val finalbottom = finaltop + childHeight

        super.layoutDecoratedWithMargins(child, left, finaltop, right, finalbottom)
    }


}

public class ThreeEqualLayoutManager : GridLayoutManager {


    val space = 12.5.dp


    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        spanCount = 3
        orientation = LinearLayoutManager.VERTICAL
    }


    override fun measureChild(child: View, widthUsed: Int, heightUsed: Int) {
        super.measureChild(child, widthUsed, heightUsed)

        var viewWidth = (width - 4 * space) / 3

        if (spanSizeLookup != null && getPosition(child) ==0&&spanSizeLookup.getSpanSize(0) == 3) {
            viewWidth = width
        }
        child.measure(
                MeasureSpec.makeMeasureSpec(viewWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(child.measuredHeight, MeasureSpec.EXACTLY)
        )

    }


}